<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class UserController extends CommonController {
    //配置
    public function config(){
        $user_object = D('user_config');
        if (IS_POST) {
            $data = $user_object->create();
            if ($data) {
                $rs = $user_object->save($data);
                if ($rs) {
                    $ajax = array(
                      'status'=>1,
                      'text'=>'用户配置保存成功',
                      'url'=>U('config')
                    );
                } else {
                    $ajax = array(
                      'status'=>0,
                      'text'=>'用户配置保存失败',
                      'url'=>''
                    );
                }
            }else {
                $ajax = array(
                  'status'=>0,
                  'text'=>$user_object->getError(),
                  'url'=>''
                );
            }
            $this->ajaxReturn($ajax);
        }else {
            $this->assign('mark','user');
            $this->assign('umark','config');
            $form_data = $user_object->find(1);
            A('form')->setMetaTitle('用户配置')
            ->setBreadCrumb(array('用户','用户管理','用户配置'))
            ->setPostUrl(U('config'))
            ->addFormItem('id','hidden')
            ->addFormItem('register_switch','switch','注册开关','注册关闭后将不能访问，后台可正常登录',array('开启','关闭'))
            ->setFormData($form_data)
            ->fetch();
        }
    }
    //列表
    public function index(){
      $user_object = D('user');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($user_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $user_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','user');
      $this->assign('umark','list');
      A('list')->setMetaTitle('用户列表')
          ->setBreadCrumb(array('用户','用户管理','用户列表'))
          ->addTopButton('add',array('title'=>'用户添加'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('avatar','头像','avatar',50,'false')
          ->addTableColumn('username','用户名','text','auto','false')
          ->addTableColumn('nickname','用户昵称','text',100,'false')
          ->addTableColumn('team_id','团队','getteam',100,'false')
          ->addTableColumn('grade_id','等级','getgrade',60)
          ->addTableColumn('money','余额','number',60)
          ->addTableColumn('rank','Rank','number',60)
          ->addTableColumn('tel','手机号码','text',100,'false')
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',200,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('forbid')
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $user_object = D('user');
      if (IS_POST) {
          $data = $user_object->create();
          if ($data) {
              $id = $user_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'用户新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'用户新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$user_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','user');
        $this->assign('umark','list');
        $gradedata = array();
        $gradata = M('user_grade')->field('id,title')->where(array('status'=>1))->select();
        foreach ($gradata as $key => $value) {
            $up['value'] = $value['id'];
            $up['title'] = $value['title'];
            array_push($gradedata,$up);
        }
        A('form')->setMetaTitle('用户添加')
        ->setBreadCrumb(array('用户','用户管理','用户列表','用户添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('avatar','image','头像')
        ->addFormItem('username','text','用户名','',array(),array('required'=>true))
        ->addFormItem('nickname','text','用户昵称','',array(),array('required'=>true))
        ->addFormItem('password','text','密码','',array(),array('required'=>true))
        ->addFormItem('team_id','select','所属团队','',$teamdata)
        ->addFormItem('grade_id','select','等级','',$gradedata)
        ->addFormItem('money','text','余额（元）','用户余额，可提现')
        ->addFormItem('rank','text','Rank','Rank分，用于排名')
        ->addFormItem('email','text','邮箱地址')
        ->addFormItem('tel','text','手机号码')
        ->addFormItem('country','text','国家')
        ->addFormItem('weibo','text','微博地址')
        ->addFormItem('github','text','github地址')
        ->addFormItem('sign','textarea','个性签名')
        ->addFormItem('anonymous','switch','隐姓埋名','',array('是','否'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('status'=>1,'money'=>'0.00','rank'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $user_object = D('user');
      if (IS_POST) {
          $data = $user_object->create();
          if (!$data['password']) {
              unset($data['password']);
          }
          if ($data) {
              $rs = $user_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'用户编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'用户编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$user_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','user');
        $this->assign('umark','list');

        $id = I('get.ids',0,'intval');
        $form_data = $user_object->find($id);
        $gradedata = array();
        $gradata = M('user_grade')->field('id,title')->where(array('status'=>1))->select();
        foreach ($gradata as $key => $value) {
            $up['value'] = $value['id'];
            $up['title'] = $value['title'];
            array_push($gradedata,$up);
        }
        A('form')->setMetaTitle('用户编辑')
        ->setBreadCrumb(array('用户','用户管理','用户列表','用户编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden','','')
        ->addFormItem('avatar','image','头像')
        ->addFormItem('username','text','用户名','',array(),array('required'=>true))
        ->addFormItem('nickname','text','用户昵称','',array(),array('required'=>true))
        ->addFormItem('password','text','密码','',array(),array('required'=>true))
        ->addFormItem('team_id','select','所属团队','',$teamdata)
        ->addFormItem('grade_id','select','等级','',$gradedata)
        ->addFormItem('money','text','余额（元）','用户余额，可提现')
        ->addFormItem('rank','text','Rank','Rank分，用于排名')
        ->addFormItem('email','text','邮箱地址')
        ->addFormItem('tel','text','手机号码')
        ->addFormItem('country','text','国家')
        ->addFormItem('weibo','text','微博地址')
        ->addFormItem('github','text','github地址')
        ->addFormItem('sign','textarea','个性签名')
        ->addFormItem('anonymous','switch','隐姓埋名','',array('是','否'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $user_object = M('user');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $user_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'用户状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'用户状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $user_object = M('user');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $user_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'用户删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'用户删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
